<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use URL;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
class UserController extends Controller
{
	//首页
    public function getIndex(Request $request)
    {
		if(Auth::check())
		{
			$user = Auth::user();
			$status = $user -> status;
			if($status == '0')//判断用户状态 为0则不让登录。
			{	
				$request->session()->flush();
				return redirect('auth/login');
			}
		}
	    return view('home.index');    	
	}
	//生成验证码
	public function getVcode(Request $request)
	{
		//生成验证码图片的Builder对象，配置相应属性
	    $builder = new CaptchaBuilder;
	    //可以设置图片宽高及字体
	    $builder->build($width = 100, $height = 40, $font = null);
	    //获取验证码的内容
	    $phrase = $builder->getPhrase();
	    session(['vcode'=>$phrase]);
	    //生成图片
	    header("Cache-Control: no-cache, must-revalidate");
	    header('Content-Type: image/jpeg');
	    $builder->output();	
	}
	/**
	 * 创建鱼塘执行
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postCreatefish(Request $request)
	{
		$id = Auth::user()->id;
		$users = DB::table('fish')->where('uid', '=', $id)->get();
		if($users){
			return back();
		}
		$data = $request -> except('_token');
		if(!empty($data->name)){
			return back();

		}

		if($request->hasFile('file')){
			// 获取文件的名字
            $filename = time().rand(100000,999999);
            // 获取文件名的后缀
            $suffix = $request->file('file')->getClientOriginalExtension();
            // 文件夹
            $dirname = './uploads/';
            // 文件名
            $files = $filename.'.'.$suffix;

           	$res =  $request->file('file')->move($dirname,$files);
            $data['file']= ltrim($dirname.$files,'.');			
		}

		$data['uid'] = $id;
		DB::table('fish')->insert($data);
		return redirect('/createrfish');
	}

	//商品列表页
	public function getSearch()
	{	
		return view('home.search');
	}

	//用户中心
	public function getCenter()
	{
		return view('home.center');
	}
	//个人资料
	public function getInformation(Request $request)
	{
		$info = Auth::user();

		// dd($info);
		return view('home.information');
	}
	/**
	 * 鱼塘创建显示
	 * @return [type] [description]
	 */
	public function getFishponds()
	{
		// $users = $this->getCatesByDiGui(0);
		$id = Auth::user()->id;
		$users = DB::table('fish')->where('uid', '=', $id)->get();
		return view('home.Fishponds',['user'=>$users]);

	}
	/**
	 * 我的鱼塘展示
	 * 修改鱼塘基本信息
	 * @return [type] [description]
	 */
	public function getCreaterfish()
	{
		$id = Auth::user()->id;
		$res = DB::table('fish')->where('uid', '=', $id)->get();
		
		return view('home.createfish',['res'=>$res]);
	}

	/**
	 * 城市三级联动
	 * 递归方式来获取分类信息
	 */
	public function getCatesByDiGui($pid)
	{
		//获取分类
		$cates = DB::table('blocks')->where('pid','=', $pid)->get();
		//遍历分类信息
		$sub_cate = [];
		foreach($cates as $k=>$v) {
			$v->sub_cate = $this->getCatesByDiGui($v->id);
			$sub_cate[] = $v;
		}
		return $sub_cate;
	}

	/**
	 * 城市三级联动
	 * 获取当前分类的所有的信息
	 */
	public function getAllCates()
	{
		//读取所有的分类信息
		//将分类信息读出来
		$cates = DB::table('blocks')
			->select(DB::raw('*, concat(path,",",id) as paths'))
			->orderBy('paths')
			->get();

		//格式化显示的分类信息
		$cates = $this->formatCateInfo($cates);
		//返回结果
		return $cates;
	}

	/**
	 * 城市三级联动
	 * 处理数据 格式化显示分类信息
	 */
	public function formatCateInfo($cates)
	{
		foreach($cates as $k=>$v) {
			//判断当前的分类的等级
			$arr = explode(',', $v->path);// 0,3,7
			$level = count($arr) - 1;
			//修改分类中的名称  |-----
			$v->name =  str_repeat('|-----', $level) . $v->name;
		}
		return $cates;
	}	



	/**
	 * 鱼塘 前台页面展示
	 * @return [type] [description]
	 */
	public function getPondfis()
	{
		// 获取鱼塘信息
		$res = DB::table('fish')->where('status','=',1)->get();
		// 获取城市信息
		$sel = DB::table('city')->where('keys','=',0)->get();
		return view('home.fishin',['res'=>$res,'sel'=>$sel]);
	}

	public function getPondsnews($id)
	{
		// 获取鱼塘信息
		$res = DB::table('fish')->where('id','=',$id)->get();
		return view('home.pondsnews',['res'=>$res]);
	}	

	public function getPondsscents()
	{
		return view('home.pondsscents');
	}
	public function getPondsshop()
	{
		return view('home.pondsshop');
	}
	public function getPondsabout()
	{
		return view('home.pondsabout');
	}
	public function getPondsblog()
	{
		return view('home.pondsblog');
	}	
	public function getPondscontact()
	{
		return view('home.pondscontact');
	}	

}
